package com.sg.service.base.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.app.req.ImplementEverYoungDispatchSpaceLoopStartReq;
import com.sg.service.base.app.MSpaceService;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.dal.mapper.mbg.SpaceMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MSpaceServiceImpl
        extends ServiceImpl<SpaceMapper, Space>
        implements MSpaceService {

    @Autowired
    SpaceMapper spaceMapper;

    /**
     * M3执行长生调度空间【循环开始】[5094]
     * gen by moon at 11/2/2022, 7:42:54 AM
     */
    @Trace(operationName = "M3执行长生调度空间【循环开始】")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Space implementEverYoungDispatchSpaceLoopStart(ImplementEverYoungDispatchSpaceLoopStartReq reqDto) {

        try {

            QueryWrapper<Space> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            if (super.list(queryWrapper) != null && super.list(queryWrapper).size() > 0) {
                Space retData = super.list(queryWrapper).get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

}

